//此程序是免费软件：您可以重新分发和/或修改
//它是根据由GNU发布的GNU较小通用公共许可证的条款
//自由软件基金会（许可的版本3）或
//（您可以选择）任何更高版本。
//
//分发了该程序，希望它会有用，
//但没有任何保证； 甚至没有默示担保
//特定用途的适销性或适用性。 看到
//有关更多详细信息，请参见GNU次通用公共许可证。
//
//您应该已经收到了GNU次级通用公共许可证
//和该程序一起 如果不是，请参见<http://www.gnu.org/licenses/>。
package org.uma.jmetal.example.multiobjective;

import org.uma.jmetal.algorithm.Algorithm;
import org.uma.jmetal.algorithm.multiobjective.cdg.CDGBuilder;
import org.uma.jmetal.example.AlgorithmRunner;
import org.uma.jmetal.operator.crossover.impl.DifferentialEvolutionCrossover;
import org.uma.jmetal.problem.doubleproblem.DoubleProblem;
import org.uma.jmetal.problem.multiobjective.glt.GLT4;
import org.uma.jmetal.solution.doublesolution.DoubleSolution;
import org.uma.jmetal.util.AbstractAlgorithmRunner;
import org.uma.jmetal.util.JMetalLogger;

import java.io.FileNotFoundException;
import java.util.List;

/**
 * *用于配置和运行CDG算法的类可以在以下位置下载论文和Matlab代码
 * http://xinyecai.github.io/
 *
 * @author Feng Zhang
 */
public class CDGRunner extends AbstractAlgorithmRunner {
  /**
   * @param args Command line arguments.
   * @throws ClassNotFoundException
   * @throws SecurityException Invoking command: java org.uma.jmetal.runner.multiobjective.CDGRunner
   *     problemName [referenceFront]
   */
  public static void main(String[] args) throws FileNotFoundException {
    DoubleProblem problem;
    Algorithm<List<DoubleSolution>> algorithm;
    DifferentialEvolutionCrossover crossover;

    String problemName;
    String referenceParetoFront = "";
    if (args.length == 1) {
      problemName = args[0];
    } else if (args.length == 2) {
      problemName = args[0];
      referenceParetoFront = args[1];
    } else {
      problemName = "(none)";
    }
    System.out.println("Warning: the problem name is not used anymore and may be removed later.");
    System.out.println("Warning: current problem name: " + problemName);

    problem = new GLT4(10);

    double cr = 1.0;
    double f = 0.5;
    crossover =
        new DifferentialEvolutionCrossover(
            cr, f, DifferentialEvolutionCrossover.DE_VARIANT.RAND_1_BIN);

    algorithm =
        new CDGBuilder(problem)
            .setCrossover(crossover)
            .setMaxEvaluations(300 * 1000)
            .setPopulationSize(300)
            .setResultPopulationSize(300)
            .setNeighborhoodSelectionProbability(0.9)
            .build();

    AlgorithmRunner algorithmRunner = new AlgorithmRunner.Executor(algorithm).execute();

    List<DoubleSolution> population = algorithm.getResult();
    long computingTime = algorithmRunner.getComputingTime();

    JMetalLogger.logger.info("Total execution time: " + computingTime + "ms");

    printFinalSolutionSet(population);
    if (!referenceParetoFront.equals("")) {
      printQualityIndicators(population, referenceParetoFront);
    }
  }
}
